//
//  SYOAuthViewController.swift
//  SYWeibo
//
//  Created by Saya on 16/4/9.
//  Copyright © 2016年 黄金果. All rights reserved.
//

import UIKit
import SVProgressHUD

class SYOAuthViewController: UIViewController ,UIWebViewDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()
        loadWebView()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        SVProgressHUD.dismiss()
    }
    
    private let webView = UIWebView()
    override func loadView() {
        view = webView
        webView.delegate = self
    }
    
    private func loadWebView (){
        //这个webView的load页面为什么没有使用网络框架?
        let urlString = "https://api.weibo.com/oauth2/authorize?" + "client_id=" + client_id + "&redirect_uri=" + redirect_uri
        webView.loadRequest(NSURLRequest(URL: NSURL(string: urlString)!))
        
        //设置左右俩个按键
        navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: nil, title: "关闭", target: self, action: "closeBtnDidClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imgName: nil, title: "自动登录", target: self, action: "automicLoad")
    }
    
    @objc private func automicLoad (){
        webView.stringByEvaluatingJavaScriptFromString("document.getElementById('passwd').value = 'huang1990'")
        webView.stringByEvaluatingJavaScriptFromString("document.getElementById('userId').value = '735163258@qq.com'")
        
    }
    
    @objc private func closeBtnDidClick (){
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    
    //MARK:webView delegate
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
        let title = webView.stringByEvaluatingJavaScriptFromString("document.title")
        navigationItem.title = title
    }
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        let requestUrlString = (request.URL?.absoluteString)!
//        let range :Range = (requestUrlString.rangeOfString("code="))!
        if requestUrlString.containsString("code="){
            let range = requestUrlString.rangeOfString("code=")
            let code:String = requestUrlString.substringFromIndex((range?.endIndex)!)
            print(code)
            SYVIewModel.viewModel.requestAccessCode(code, finishBlock: { (isSucceed:Bool) -> () in
                if !isSucceed {
                    SVProgressHUD.showErrorWithStatus("对不起您的网速不给力哦")
                    return
                }
//                SVProgressHUD.showSuccessWithStatus("第三方登录成功,即将自动登录")
                self.dismissViewControllerAnimated(false, completion: { () -> Void in
                    NSNotificationCenter.defaultCenter().postNotificationName(SYSwitchRootController, object: "SYWelcomVC")
                })
                
            })
            return false
        }
        return true
    }
}












